package Game;

import java.util.Random;

public class GameLogic {
    private GameState state;
    private GameRules rules;
    private String[][] map;
    private int playerX, playerY;
    private Random rand;

    public GameLogic(int bestScore) {
        state = new GameState(bestScore);
        rules = new MiningGameRules();
        rand = new Random();
        setupMap();
    }
    // 初始化地图
    private void setupMap() {
        int size = rules.getMapSize(state.getLevel());
        map = new String[size][size];

        // 先将游戏中格子全部放空
        for(int i=0; i<size; i++) {
            for(int j=0; j<size; j++) {
                map[i][j] = " ";
            }
        }
        // 随机放墙
        for(int i=0; i<size; i++) {
            for(int j=0; j<size; j++) {
                if(rand.nextDouble() < rules.getWallChance()) {
                    map[i][j] = rules.getWall();
                }
            }
        }
        // 放人
        placePlayer();
        placeMinerals();

        if(rand.nextDouble() < 0.3) {
            placeFixSpot();
        }
        placeDownStair();
    }
    private void placePlayer() {
        int size = map.length;
        do {
            playerX = rand.nextInt(size);
            playerY = rand.nextInt(size);
        } while(!map[playerY][playerX].equals(" "));

        map[playerY][playerX] = "人";
    }

    // 随机放矿
    private void placeMinerals() {
        String[] minerals = rules.getAllMinerals();
        int count = map.length * 2; // 矿物数量

        for(int i=0; i<count; i++) {
            int x, y;
            do {
                x = rand.nextInt(map.length);
                y = rand.nextInt(map.length);
            } while(!map[y][x].equals(" "));

            String mineral = minerals[rand.nextInt(minerals.length)];
            map[y][x] = mineral;
        }
    }

    // 随机修理点位置和出现位置
    private void placeFixSpot() {
        int x, y;
        do {
            x = rand.nextInt(map.length);
            y = rand.nextInt(map.length);
        } while(!map[y][x].equals(" "));

        map[y][x] = "修";
    }

    // 放置下一层矿洞
    private void placeDownStair() {
        int x, y;
        do {
            x = rand.nextInt(map.length);
            y = rand.nextInt(map.length);
        } while(!map[y][x].equals(" "));

        map[y][x] = rules.getDown();
    }

    // 移动玩家
    public boolean movePlayer(int dx, int dy) {
        int newX = playerX + dx;
        int newY = playerY + dy;

        // 检查边界
        if(newX < 0 || newX >= map.length || newY < 0 || newY >= map.length) {
            return false;
        }

        // 检查是否被墙阻碍（检查碰撞）
        if(map[newY][newX].equals(rules.getWall())) {
            return false;
        }

        // 移动玩家
        map[playerY][playerX] = " ";
        playerX = newX;
        playerY = newY;
        String cell = map[playerY][playerX];
        map[playerY][playerX] = "人";

        // 处理不同格子
        if(cell.equals("修")) {
        // 修理矿稿
            int newDura = state.getDurability() + rules.getFixAmount();
            if(newDura > rules.getStartDurability()) {
                newDura = rules.getStartDurability();
            }
            state.setDurability(newDura);
        }
        else if(cell.equals(rules.getDown())) {
        // 进入下一层
            nextLevel();
        }
        else if(!cell.equals(" ")) {
        // 挖矿
            int cost = rules.getCost(cell);
            state.setDurability(state.getDurability() - cost);

        // 计算分数
            int score = rules.countScore(state.getLevel(), 1);
            state.addScore(score);

        // 检查游戏结束
            if(rules.checkGameOver(state.getDurability())) {
                state.setOver(true);
            }
        }

        return true;
    }

    // 进入下一层
    private void nextLevel() {
        if(state.getLevel() < rules.getMaxLevels()) {
            state.setLevel(state.getLevel() + 1);
            setupMap();
        }
    }

    // 随机生成地图
    public String[][] getMap() {
        return map;
    }

    // 获取游戏状态
    public GameState getState() {
        return state;
    }

    // 获取规则
    public GameRules getRules() {
        return rules;
    }
}
